// 5.快速排序
// 快速排序法的时间复杂度是 O(nlogn)
// 1.取基准值为数组最左边的数

function quickSort(arr, left = 0, right = arr.length - 1) {
  if (left >= right) return;
  let baseId = left,
    baseVal = arr[baseId],
    i = left,
    j = right;
  while (i < j) {
    while (j > i && arr[j] >= baseVal) {
      j--;
    }
    while (i < j && arr[i] <= baseVal) {
      i++;
    }
    [arr[i], arr[j]] = [arr[j], arr[i]];
  }
  [arr[baseId], arr[i]] = [arr[i], arr[baseId]];
  quickSort(arr, left, i - 1);
  quickSort(arr, i + 1, right);
  return arr;
}

const test = [765, 234, 547, 241, 62, 124, 6, 232, 3, 2];
console.log(quickSort(test));
